#include "gameview.h"
#include "gamescene.h"
#include <QGLWidget>

GameView::GameView(QWidget * parent) : QGraphicsView(parent)
{
     setMinimumSize(800, 600);
     setWindowTitle(tr("Tower Defence"));
     setWindowIcon(QIcon(":/images/icon.png"));
     setWindowFlags(windowFlags() ^ Qt::WindowMaximizeButtonHint); // disable maximize

     // if accelerated
     setViewport(new QGLWidget( QGLFormat(QGL::SampleBuffers)));
     setViewportUpdateMode( QGraphicsView::SmartViewportUpdate);

     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

     setFocusPolicy(Qt::StrongFocus);
    // Forward the key press events to the Game instance
    //connect(this, SIGNAL(keyPressed(QKeyEvent*)), p_game, SLOT(keyPressEvent(QKeyEvent*)));
}

GameView::~GameView()
{
}

void GameView::resizeEvent(QResizeEvent * e)
{
    /*if (height() != (3.0 / 4.0) * width())
    {
        QSize diffsize = e->size() - e->oldSize();
        if (qAbs(diffsize.width()) > qAbs(diffsize.height()))
        {
            resize(e->size().width(), e->size().width() * (3.0 / 4.0));
        }
        else
        {
            resize(e->size().height() / (3.0 / 4.0), e->size().height() );
        }
    }*/
    //if (scene())
    //{
        //scene()->setSceneRect(QRectF(QPointF(0, 0), e->size()));
      //  e->accept();
    //}
    //QGraphicsView::resizeEvent(e);
    fitInView(sceneRect(), Qt::KeepAspectRatio);
}

void GameView::keyPressEvent(QKeyEvent* p_event)
{
        emit(keyPressed(p_event));
}

void GameView::focusOutEvent(QFocusEvent*)
{
        // Pause the game if it is not already paused
        //if (((GameScene*)scene())->getGame()->getTimer()->isActive()) {
        //	((GameScene*)scene())->getGame()->switchPause();
        //}
}

void GameView::newGame()
{
    setScene(new GameScene(this));
}
